<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    var a = [1, [2, [3, 4, 5]]];

    function flatten(arr) {
      let result = [];

      for (let i = 0; i < arr.length; i++) {
        //   如果检测到元素还是数组，那么就递归调用函数每一次调用函数，都会有一个闭包函数，result对应的是该闭包的取得值，之后递归回归在逐级添加到result
        // 把递归调用思想方法抽象出来即可
        if (Array.isArray(arr[i])) {
          result = result.concat(flatten(arr[i]));
        } else {
          result.push(arr[i]);
        }
      }
      return result;
    }
    flatten(a);
  </script>
</html>
